package com.quick.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.quick.base.Page;
import com.quick.base.Search;
import com.quick.mapper.SysDeptMapper;
import com.quick.service.ISysDeptService;
import com.quick.sys.domain.dto.SysDeptDTO;
import com.quick.sys.domain.pojo.SysDept;
import com.quick.sys.domain.search.SysDeptSearch;
import com.quick.sys.domain.vo.SysDeptVo;
import com.quick.sys.domain.vo.SysPostVo;
import com.quick.util.CallBack;
import com.quick.util.Convert;
import com.quick.util.PageUtil;
import com.quick.util.QuickTool;

import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class SysDeptServiceImpl implements ISysDeptService {

    private final SysDeptMapper sysDeptMapper;
    @SuppressWarnings("unchecked")
	@Override
    public Page<SysDeptVo> search(Search<SysDeptSearch> search) {
        PageHelper.startPage(search.getPageNum(), search.getPageSize());
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper(queryWrapper,search.getSearch());
        List<SysDept> list = sysDeptMapper.selectList(queryWrapper);
        @SuppressWarnings("rawtypes")
		PageInfo<?> pageInfo = new PageInfo(list);
        return (Page<SysDeptVo>) PageUtil.page(pageInfo, SysDeptVo.class, new CallBack() {
            @Override
            public List<?> handle(List<?> list) {
                List<SysDeptVo> res = Convert.convert(list,SysDeptVo.class);
                List<Integer> ids = res.stream().map(SysDeptVo::getParentId).collect(Collectors.toList());
                Map<Integer, SysDeptVo> deptVoMap = loadDept(ids);
                res.forEach(e->{
                    e.setParentName(deptVoMap.getOrDefault(e.getParentId(),new SysDeptVo()).getName());
                });
                return res;
            }
        });
    }
	@Override
    public List<SysDeptVo> deptByIds(List<Integer> deptIds) {
        return null;
    }

    @Override
    public Map<Integer, SysDeptVo> loadDept(List<Integer> deptIds) {
        QueryWrapper<SysDept> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(SysDept::getId,deptIds);
        List<SysDept> list = sysDeptMapper.selectList(queryWrapper);
        List<SysDeptVo>  deptVos = Convert.convert(list,SysDeptVo.class);
        return deptVos.stream().collect(Collectors.toMap(SysDeptVo::getId,o->o));
    }
	@Override
	public Boolean saveSysDept(SysDeptDTO role) {
		SysDept entity = Convert.convert(role, SysDept.class);
		if(QuickTool.isNull(role.getId())) {
			QuickTool.setFillInsert(entity);
			return sysDeptMapper.insert(entity)>0;
		}
		QuickTool.setFillUpdate(entity);
		return sysDeptMapper.updateById(entity)>0;
	}
	@Override
	public SysPostVo queryById(Long id) {
		SysDept dept =sysDeptMapper.selectById(id);
		return Convert.convert(dept, SysPostVo.class);
	}
	@Override
	public Boolean delById(Long id) {
		return sysDeptMapper.deleteById(id)>0;
	}
	@Override
	public Boolean batchDel(List<Long> ids) {
		return sysDeptMapper.deleteBatchIds(ids)>0;
	}
}
